package com.gwsdk.paysdk.acitvityV2.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gwsdk.paysdk.acitvityV2.adapter.GWCouponAdapter;
import com.gwsdk.paysdk.bean.CouponBean;
import com.gwsdk.paysdk.bean.RebateBean;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.http.process.CouponAllProcess;
import com.gwsdk.paysdk.utils.AppUtils;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.TextUtils;
import com.gwsdk.paysdk.utils.ToastUtil;
import com.lidroid.xutils.BitmapUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressLint("ValidFragment")
public class GWCouponFragment extends Fragment implements View.OnClickListener{

    private TextView chongzifanli;
    private TextView daijingjuan;

    private RelativeLayout scrollView;
    private ListView listView;
    private RelativeLayout couponView;
    private List<CouponBean> listData;
    private GWCouponAdapter adapter;
    private WebView webView;
    private int type = 1;

    private View rootView;

    private RebateBean rebateBean;

    public GWCouponFragment(){

    }

    public GWCouponFragment(int type){
        this.type = type;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(setContentView(), container, false);
        initView(rootView);
        getData();
        return rootView;
    }

    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_coupon");
    }

    public void initView(View view) {
        chongzifanli = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_chongzi_fanli_btn"));
        daijingjuan = view.findViewById(GWInflaterUtils.getControl(getActivity(),"gw_daijingjuan_btn"));
        chongzifanli.setOnClickListener(this);
        daijingjuan.setOnClickListener(this);

        webView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"web_view"));
        couponView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"coupon_view"));
        scrollView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"scroll_view"));
        listView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"list_view"));
        adapter = new GWCouponAdapter(getActivity(),handler,false);
        adapter.setType(type);
        listView.setAdapter(adapter);

        webView.setBackgroundColor(0);
        webView.getBackground().setAlpha(0);
        webView.getSettings().setJavaScriptEnabled(true);
    }

    public void getData() {

        CouponAllProcess process = new CouponAllProcess();
        process.setType(1);
        process.setIsbind("0");
        process.post(handler);
        process.getRebate(handler);

    }

    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constant.ALL_COUPON_SUCCESS:
                    listData = (List<CouponBean>) msg.obj;
                    if (listData != null && listData.size() > 0) {
                        adapter.setData(listData);
                        listView.setVisibility(View.VISIBLE);
                        rootView.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view_1")).setVisibility(View.GONE);
                    } else {
                        listView.setVisibility(View.GONE);
                        rootView.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view_1")).setVisibility(View.VISIBLE);
                    }
                    break;
                case Constant.ALL_COUPON_FAIL:
                    listView.setVisibility(View.GONE);
                    rootView.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view_1")).setVisibility(View.VISIBLE);

                    if (!TextUtils.isEmpty(msg.obj.toString())) {
                        ToastUtil.show(getActivity(), msg.obj.toString());
                    }
                    break;
                case Constant.GET_REBATE_SUCCESS:
                    scrollView.setVisibility(View.VISIBLE);
                    rebateBean = (RebateBean) msg.obj;
                    String value1 = translation(rebateBean.recharge_introduction);
                    String value2 = translation(rebateBean.recommend_content);
                    if (!TextUtils.isEmpty(value1) || !TextUtils.isEmpty(value2)){
                        String html = loadWebViewHtml(value1,value2);
                        webView.setVisibility(View.VISIBLE);
                        webView.loadDataWithBaseURL(null,html, "text/html",  "utf-8", null);
                        rootView.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view"))
                                .setVisibility(View.GONE);
                    }else {
                        webView.setVisibility(View.GONE);
                        rootView.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view")).setVisibility(View.VISIBLE);
                    }
                    break;
                case Constant.GET_REBATE_FAIL:
                    rootView.findViewById(GWInflaterUtils.getControl(getActivity(),"not_data_view")).setVisibility(View.VISIBLE);
                    break;
            }
        }
    };

    private String loadWebViewHtml(String value,String value2){

        String html = "<!DOCTYPE html>\n" +
                "<html lang=\"zh\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0\">\n" +
                "    <style type='text/css'>\n" +
                "        body,.aui-content{background-color: black;}\n" +
                "        h2{font-size: 17px;}\n" +
                "        h2,p{color: white;margin-bottom: 10px;}\n" +
                "        .content{background-color: #ffffff25;padding: 10px;border-radius: 5px;}\n" +
                "        img{max-width:100%;}\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div class=\"aui-content\" >\n";
        if (!TextUtils.isEmpty(value)){
            html += "    <div id=\"gw\">\n" +
                    "        <h2>跟玩返利</h2>\n" +
                    "        <div id=\"gw_text\"  class=\"content\">"+value+"</div>\n" +
                    "    </div>\n";
        }
        if (!TextUtils.isEmpty(value2)){
            html += " <div id=\"game\">\n" +
                    "        <h2>游戏返利</h2>\n" +
                    "        <div id=\"game_text\" class=\"content\">"+value2+"</div>\n" +
                    "  </div>\n";
        }
        html += "</div>\n"+
                "</body>\n" +
                "</html>";
        return html;
    }


    private String translation(String content) {
        if (TextUtils.isEmpty(content)){
            return content;
        }
        String str1=content;
        str1 = str1.replace("&amp;", "&");
        str1 = str1.replace("&lt;", "<");
        str1 = str1.replace("&gt;", ">");
        str1 = str1.replace("&quot;", "'");
        str1 = str1.replace("&nbsp;", " ");
        return str1;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == GWInflaterUtils.getControl(getActivity(),"gw_chongzi_fanli_btn")) {
            chongzifanli.getPaint().setFakeBoldText(true);
            daijingjuan.getPaint().setFakeBoldText(false);
            chongzifanli.setTextColor(Color.parseColor("#14B6FC"));
            daijingjuan.setTextColor(Color.parseColor("#E4E4E4"));
            scrollView.setVisibility(View.VISIBLE);
            couponView.setVisibility(View.GONE);
        }else if (view.getId() == GWInflaterUtils.getControl(getActivity(),"gw_daijingjuan_btn")) {
            chongzifanli.getPaint().setFakeBoldText(false);
            daijingjuan.getPaint().setFakeBoldText(true);
            chongzifanli.setTextColor(Color.parseColor("#E4E4E4"));
            daijingjuan.setTextColor(Color.parseColor("#14B6FC"));
            scrollView.setVisibility(View.GONE);
            couponView.setVisibility(View.VISIBLE);
        }
    }
}
